#pragma once
#include <sys/types.h>
#include <sys/epoll.h>
#include "Socket.hpp"
#include "log.hpp"
const int size = 128;
class Epoller
{
public:
    Epoller()
    {
        epfd_ = epoll_create(size);
        if (epfd_ < 0)
        {
            lg(Error, "epoll_creat error,errno:%d,strerr:%s", errno, strerror(errno));
        }
        else
        {
            lg(Info, "epoll_creat success,epfd:%d", epfd_);
        }
    }

    int Epoller_wait(struct epoll_event revent[], int num,int timeout)
    {
        int n = epoll_wait(epfd_, revent, num, timeout);
        // std::cout<<"n"<<n<<std::endl;
        if(n>0)
            std::cout<<"epoll_wait got"<<std::endl;
        return n;
    }

    int EpollerUpdata(int op,int fd,uint32_t event)
    {
        int n=0;
        if(op==EPOLL_CTL_DEL)
        {
            n=epoll_ctl(epfd_,op,fd,nullptr);
            if(n!=0)
            {
                lg(Error,"epoll_ctl error,errno:%d,strerr:%s",errno,strerror(errno));
            }
        }
        else //EPOPLL_CTL_ADD  EEPOLL_CTL_MOD
        {
            struct epoll_event ev;
            ev.events=event;
            ev.data.fd=fd; //方便我们后续知道哪个错了
            n=epoll_ctl(epfd_,op,fd,&ev);
            if(n!=0)
            {
                lg(Error,"epoll_ctl error,errno:%d,strerr:%s",errno,strerror(errno));
            }
        }
        return n;
    }
    ~Epoller()
    {
        if (epfd_ >= 0)
            close(epfd_);
    }

private:
    int epfd_;
    int timeout = {3000};
};